package org.ricks.common.conf;


import java.util.HashMap;
import java.util.Map;

/**
 * @author chenwei
 * @Title:
 * @Package
 * @Description:
 * @date 2020/8/717:06
 */
public class SourceHandler {


    private Map<Class<?>, Object> sourceMap = new HashMap<>();

    private SourceHandler() {
    }

    private static class SourceHandlerInner {
        private static SourceHandler sourceHandler = new SourceHandler();
    }

    public static SourceHandler getSourceHandler() {
        return SourceHandlerInner.sourceHandler;
    }

    public Object getSourceByClass(Class<?> clazz) {
        return sourceMap.get(clazz);
    }

    public Object getMainSource() {
        return MainConfLoader.getLoader().getMainConfValues();
    }

    public void loadSource() {
        MainConfLoader.getLoader().load();
        fillSourceMap();
    }

    private void fillSourceMap() {
        Map<Class<?>, LoadFileName> fixedClassMap = org.ricks.common.conf.ClassHandler.getClassHandler().getFixedClassMap();

        // 首先将fixedClass和它对应的confSource都加入
        fixedClassMap.forEach((clazz, loadFileName) -> {
            Object tmpSource = MainConfLoader.getLoader().getOriginMainConfValues(); // 初始的
            sourceMap.putIfAbsent(clazz, tmpSource);
        });

    }

}

